﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using Db4objects.Db4o.Linq;
using SharedObjects;

namespace TheGameDatabaseTool
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow
	{
		public MainWindow()
		{
			InitializeComponent();
		}

		void DbmainOperationCompleted(object sender, EventArgs e)
		{
			App.Current.Dispatcher.BeginInvoke(new ThreadStart(() => VisualStateManager.GoToElementState(LayoutRoot, "Idle", true)));
		}

		private void BtnWipeDbClick(object sender, RoutedEventArgs e)
		{
			if (MessageBox.Show(this, "Confirm WIPING the database:", "Confirm", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
				return;
			Thread.Sleep(1000);
			if (MessageBox.Show(this, "Confirm WIPING the database again:", "Confirm", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
				return;
			VisualStateManager.GoToElementState(LayoutRoot, "Busy", true);
			new Thread(() =>
			{
				Stopwatch sw = new Stopwatch();
				sw.Start();
				DatabaseMainScript dbmain = new DatabaseMainScript(true);
				dbmain.OperationCompleted += DbmainOperationCompleted;
				dbmain.CreateWorld();
				sw.Stop();
				MessageBox.Show("CreateWorld finished in " + sw.Elapsed);
				//var nulls = (from EntityBase b in dbmain.Db where b.Id == 0 select b);
				dbmain.Close();
			}).Start();
		}

		private void BtnUpdateDbClick(object sender, RoutedEventArgs e)
		{
			VisualStateManager.GoToElementState(LayoutRoot, "Busy", true);
			new Thread(() =>
			{
				Stopwatch sw = new Stopwatch();
				sw.Start();
				DatabaseMainScript dbmain = new DatabaseMainScript(false);
				dbmain.OperationCompleted += DbmainOperationCompleted;
				var oldEntities = (from EntityBase b in dbmain.Db select b).ToList();
				dbmain.CreateWorld();
				#region verification code
				//dbmain.CreateWorld();
				var newEntities = (from EntityBase b in dbmain.Db select b).ToList();
				//var diff = newEntities.Except(oldEntities).ToList();
				//sw.Stop();
				//MessageBox.Show("CreateWorld finished in " + sw.Elapsed);
				//dbmain.Testing();
				//var programs = (from Program p in dbmain.db select p);
				#endregion
				dbmain.Close();
			}).Start();
		}

		private void CreateListFiles()
		{
			List<string> corpNames = new List<string>(File.ReadAllLines("..\\..\\CorpNames.txt", Encoding.ASCII));
			List<string> mod = corpNames.Select(s => string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12}", s, Extension.ToLong(Guid.NewGuid()), Extension.ToLong(Guid.NewGuid()), Extension.ToLong(Guid.NewGuid()), Extension.ToLong(Guid.NewGuid()), Extension.ToLong(Guid.NewGuid()), Extension.ToLong(Guid.NewGuid()), Extension.ToLong(Guid.NewGuid()), Extension.ToLong(Guid.NewGuid()), Extension.ToLong(Guid.NewGuid()), Extension.ToLong(Guid.NewGuid()), Extension.ToLong(Guid.NewGuid()), Extension.ToLong(Guid.NewGuid()))).ToList();
			File.WriteAllLines("..\\..\\CorpNamesGuids.txt", mod.ToArray());
		}

		private void BtnMigrationClick(object sender, RoutedEventArgs e)
		{
			VisualStateManager.GoToElementState(LayoutRoot, "Busy", true);
			new Thread(() =>
			{
				DatabaseMainScript dbmain = new DatabaseMainScript(false);
				dbmain.OperationCompleted += DbmainOperationCompleted;
				dbmain.Migration();
				dbmain.Close();
			}).Start();
		}
	}
}
